#include <iostream>
#include <string>

#include <gloox/gloox.h>
#include <gloox/client.h>
#include <gloox/message.h>
#include <gloox/messagehandler.h>
#include <gloox/connectionlistener.h>

#include "gloox_in_c.h"

extern "C" {
    extern void log_handler(int, int, char*);
    extern void message_handler(int, char*, char*);
};

using namespace std;
using namespace gloox;


class DemoLogHandler2 : public LogHandler {
public:
    virtual void handleLog(LogLevel level, LogArea area, const std::string& message) {
        // cout << level << area << message << endl;
        log_handler(int(level), int(area), (char*)message.c_str());
    }
};

class DemoMessageHandler : public MessageHandler{
public:
    virtual void handleMessage( const Message& stanza, MessageSession* session = 0 ) {
        // cout << "Received message: " << stanza << endl;
        // Message msg(Message::Chat, stanza.from(), "Tell me more about " + stanza.body() );
        // client->send( msg );
        message_handler(int(stanza.subtype()),
                        (char*)stanza.from().full().c_str(),
                        (char*)stanza.body( ).c_str());
    }
};

void* gloox_JID_new(char *jid){
    auto *jido = new JID(jid);
    return jido;
}
void* gloox_client_new(void* jid, char *password) {
    auto *cli = new Client(*(JID*)jid, password);
    auto *logh = new DemoLogHandler2();
    cli->logInstance().registerLogHandler(LogLevelDebug, LogAreaAll, logh);
    auto *msgh = new DemoMessageHandler();
    cli->registerMessageHandler(msgh);
    return cli;
}

int gloox_client_connect(void *cli, int block) {
    return ((Client*)cli)->connect(block==0?false:true);
}
